﻿using UnityEngine;
using System.Collections;

public class WindowBase : MonoBehaviour
{
	/// <summary>
	/// The tag represents the gameobject for UI.
	/// </summary>
	public string Tag;
	/// <summary>
	/// The gameobject has a component called "UIPanel"
	/// </summary>
	protected GameObject m_panelGO;
	protected UIPanel    m_panelSp;

	void Awake()
	{
		UIHub.Instance.Register(this);
	}

	void RefreshPanel()
	{
		m_panelSp.Refresh();
	}

	void OnDestroy()
	{
		UIHub.Instance.UnRegister(this);
	}
	
	public virtual void Link(GameObject obj)
	{
		if(obj == null)
		{
			throw new System.NullReferenceException();
		}
		
		m_panelGO = obj;
		m_panelGO.SetActive(false);
		m_panelSp = obj.GetComponent<UIPanel>();
		
		BoxCollider collider = obj.GetComponent<BoxCollider>();
		if(collider != null)
			collider.size = new Vector3(UIHub.ScreenW, UIHub.ScreenH, 0);
		
		UIButtonMessage[] bms = obj.GetComponentsInChildren<UIButtonMessage>(true);
		foreach(UIButtonMessage bm in bms )
		{
			bm.target = gameObject;
		}
		
		enabled =false;
	}
}
